package com.example.home.fragment

import android.util.Log
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.GsonUtils
import com.example.common.mvvm.BaseMvvmFragment
import com.example.home.R
import com.example.home.adapter.NominateListAdapter
import com.example.home.databinding.FragmentNominateBinding
import com.example.home.databinding.ItemHomeBannerViewBinding
import com.example.entity.BaseEntity
import com.example.home.uistate.HomeUiState
import com.example.home.viewmodel.HomeViewModel
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class NominateFragment : BaseMvvmFragment<FragmentNominateBinding>() {
    private val mViewModel :HomeViewModel by activityViewModels()

    private  var mAdapter : NominateListAdapter ? = null

    private var isLoadMore = false
    private lateinit var bannerView: View

    companion object {
        @JvmStatic
        fun newInstance() =NominateFragment()
    }



    override fun getLayoutId(): Int=R.layout.fragment_nominate

    override fun initViews() {
        mDataBinding.recyclerView.layoutManager=LinearLayoutManager(requireContext(),LinearLayoutManager.VERTICAL,false)
        mAdapter= NominateListAdapter()

        //获取Banner布局View
        bannerView = DataBindingUtil.inflate<ItemHomeBannerViewBinding>(layoutInflater, R.layout.item_home_banner_view, mDataBinding.recyclerView, false).root
        mAdapter?.setHeaderView(bannerView)

        mDataBinding.recyclerView.adapter=mAdapter



        //动态设置刷新头和加载更多
        mDataBinding.refreshLayout.setRefreshHeader(ClassicsHeader(requireContext()))
        mDataBinding.refreshLayout.setRefreshFooter(ClassicsFooter(requireContext()))
        //设置下拉事件和加载更多事件
        mDataBinding.refreshLayout.setOnRefreshListener {
            isLoadMore=false
            mViewModel.loadNominateData(isLoadMore)
            //mDataBinding.refreshLayout.finishRefresh()
        }
        mDataBinding.refreshLayout.setOnLoadMoreListener {
            isLoadMore=true
            mViewModel.loadNominateData(isLoadMore)
           // mDataBinding.refreshLayout.finishLoadMore()
        }


    }


    /**
     * 懒加载数据
     * */
    override fun lazyLoadData() {
        if (mAdapter?.data?.size==null){
            mViewModel.loadNominateData()
        }

    }

    override fun setDataObserver() {
        mViewModel.mLiveData.observe(this){
            when(it){
                is HomeUiState.loadNominateSuccess->upDataUi(it.data as MutableList<BaseEntity>)
                is HomeUiState.loadNominateFail-> showMsg(it.error)
                else -> {}
            }
        }

    }

    private fun upDataUi(list: List<BaseEntity>) {

        if (!isLoadMore){
            mAdapter?.setNewInstance(list as MutableList<BaseEntity>)
           if (mDataBinding.refreshLayout.isRefreshing){
               mDataBinding.refreshLayout.finishRefresh()
           }
        }else{
            mAdapter?.addData(list)
            if (mDataBinding.refreshLayout.isLoading){
                mDataBinding.refreshLayout.finishLoadMore()
            }
        }
        Log.d("ssss", "upDataUi: "+GsonUtils.toJson(list))


    }

}
